package com.gzist.elderlyserviceplatform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzist.elderlyserviceplatform.entity.dto.ServiceReservationQueryDTO;
import com.gzist.elderlyserviceplatform.entity.dto.ServiceReservationReviewDTO;
import com.gzist.elderlyserviceplatform.entity.po.ServiceReservation;
import com.gzist.elderlyserviceplatform.entity.vo.HomePageInfoCountVO;
import com.gzist.elderlyserviceplatform.entity.vo.HomePageInfoCountVOO;
import com.gzist.elderlyserviceplatform.entity.vo.ServiceReservationVO;
import com.gzist.elderlyserviceplatform.mapper.ServiceReservationMapper;
import com.gzist.elderlyserviceplatform.service.ServiceReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 服务预约表 服务实现类
 */
@Service
@RequiredArgsConstructor
public class ServiceReservationServiceImpl extends ServiceImpl<ServiceReservationMapper, ServiceReservation> implements ServiceReservationService {

    private final ServiceReservationMapper serviceReservationMapper;

    @Override
    public void reviewServiceReservation(ServiceReservationReviewDTO reviewDTO) {
        LambdaUpdateWrapper<ServiceReservation> wrapper = Wrappers.lambdaUpdate();
        if (StrUtil.isNotBlank(reviewDTO.getId())) {
            wrapper.eq(ServiceReservation::getId, reviewDTO.getId());
        }
        if (StrUtil.isNotBlank(reviewDTO.getReviewStatus())) {
            wrapper.set(ServiceReservation::getReviewStatus, reviewDTO.getReviewStatus());
        }
        if (StrUtil.isNotBlank(reviewDTO.getReviewOpinion())) {
            wrapper.set(ServiceReservation::getReviewOpinion, reviewDTO.getReviewOpinion());
        }
        // update拦截未触发，需要手动更新
        wrapper.set(ServiceReservation::getUpdateTime, LocalDateTime.now());
        this.update(wrapper);
    }

    @Override
    public Page<ServiceReservationVO> queryPage(ServiceReservationQueryDTO queryDTO) {
        Page<ServiceReservationVO> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        return serviceReservationMapper.queryPage(page, queryDTO);
    }

    @Override
    public List<HomePageInfoCountVOO> countServiceReservation() {
        List<HomePageInfoCountVOO> finalResult = new ArrayList<>();
        // 查询各服务以及其预约人数集合
        List<HomePageInfoCountVO> queryResult = serviceReservationMapper.countServiceReservation();
        // 进行最终结果封装
        queryResult.stream().forEach(item -> {
            HomePageInfoCountVOO homePageInfoCountVOO = new HomePageInfoCountVOO();
            BeanUtil.copyProperties(item, homePageInfoCountVOO);
            homePageInfoCountVOO.setColor(generateRandomColor());
            finalResult.add(homePageInfoCountVOO);
        });
        return finalResult;
    }

    /**
     * 生成并返回一个随机的RGB颜色代码
     * 颜色代码以十六进制字符串形式表示，格式为"#RRGGBB"
     */
    private String generateRandomColor() {
        // 生成RGB三个通道的随机值（0-255）
        int red = ThreadLocalRandom.current().nextInt(256);
        int green = ThreadLocalRandom.current().nextInt(256);
        int blue = ThreadLocalRandom.current().nextInt(256);
        // 格式化为6位十六进制字符串，补零并转大写
        return String.format("#%02X%02X%02X", red, green, blue);
    }

}